package com.douqu.game.mergeManager.mergeUtils;

import com.douqu.game.MainFrame;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import java.awt.*;

/**
 * @author: Gavin.
 * Description:
 * Date: 2018/6/8 0008 下午 3:49
 * Huan Yu Copyright (c) 2017 All Rights Reserved.
 */
public class EvenOddRowCellRenderer extends AbstractTableModel {

    public static final Object[] TITLE_ARRAY = new Object[]{"ip", "端口", "数据库名" ,"账号" ,"密码" };

    private Object[][] columns;

    private static Font font18 = new Font("宋体", 0, 18);

    DefaultTableModel tmodel;

    private int number;

    private JTable table;
    public EvenOddRowCellRenderer(int number,int y, Container container,MainFrame frame) {

        this.number = number;

        JLabel targetListTitle = new JLabel();
        targetListTitle.setBounds(20,y + 10,600,30);
        targetListTitle.setFont(font18);
        targetListTitle.setForeground(new Color(0, 0, 0));
        targetListTitle.setText("<html><font color='white'>" +
                " &nbsp; &nbsp;ip &nbsp; &nbsp; &nbsp; 端口 &nbsp; &nbsp; 数据库名 &nbsp; &nbsp; 用户名 &nbsp; &nbsp; &nbsp;密码 " +
                "</font></html>");
        container.add(targetListTitle);
        numberColumns(number);
        tmodel = new DefaultTableModel(columns,TITLE_ARRAY);
        table = new JTable(tmodel);
        table.setDefaultRenderer(Object.class, new MyRenderer());
        table.setBounds(20, y + 20 + 30, 600, 30 * number);
        table.setRowHeight(30);
        container.add(table);
    }

    private void numberColumns(int number){
        Object[][] objects = new Object[number][];
        for (int i = 0; i < number;i ++){
            objects[i] = new Object[]{"" , "" ,"" ,"" ,"" };
        }
        columns = objects;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public JTable getTable() {
        return table;
    }

    public void setTable(JTable table) {
        this.table = table;
    }

    @Override
    public int getRowCount() {
        return columns.length;
    }

    @Override
    public int getColumnCount() {
        return TITLE_ARRAY.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        return columns[rowIndex][columnIndex];
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        columns[rowIndex][columnIndex] = aValue;
        fireTableCellUpdated(rowIndex,columnIndex);
    }
}